package com.bocai.freeman.view.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bocai.freeman.R;
import com.bocai.freeman.base.BaseFragment;
import com.bocai.freeman.view.adapter.VpAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 最近Frag
 */
public class LatelyFrag extends BaseFragment implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {


    View view;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.rg_choose)
    RadioGroup rgChoose;
    @Bind(R.id.vp)
    ViewPager vp;
    @Bind(R.id.rbtn_person)
    RadioButton rbtnPerson;
    @Bind(R.id.rbtn_msg)
    RadioButton rbtnMsg;

    private VpAdapter mVpAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_lately, null);
        ButterKnife.bind(this, view);
        toolbar.inflateMenu(R.menu.menu_screen);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                showToast("筛选");
                return false;
            }
        });
        initView();
        initEvent();
        return view;
    }



    @Override
    public void initEvent() {
        super.initEvent();
        vp.setOnPageChangeListener(this);
        rgChoose.setOnCheckedChangeListener(this);
    }

    private void initView() {
        mVpAdapter = new VpAdapter(getActivity().getSupportFragmentManager());
        LatelyPersonFrag latelyPersonFrag = new LatelyPersonFrag();
        LatelyMessageFrag latelyMessageFrag = new LatelyMessageFrag();

        mVpAdapter.addFragment(latelyPersonFrag);
        mVpAdapter.addFragment(latelyMessageFrag);
        vp.setAdapter(mVpAdapter);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                if (!rbtnPerson.isChecked())
                    rbtnPerson.setChecked(true);
                break;
            case 1:
                if (!rbtnMsg.isChecked())
                    rbtnMsg.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rbtn_person:
                vp.setCurrentItem(0);
                break;
            case R.id.rbtn_msg:
                vp.setCurrentItem(1);
                break;
        }
    }
}

